package org.csu.gp_store.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.gp_store.bo.ItemBO;
import org.csu.gp_store.entity.Company;
import org.csu.gp_store.entity.Evaluation;
import org.csu.gp_store.entity.Item;
import org.csu.gp_store.entity.Product;
import org.csu.gp_store.mapper.CompanyMapper;
import org.csu.gp_store.mapper.ItemMapper;
import org.csu.gp_store.mapper.ProductMapper;
import org.csu.gp_store.service.EvaluationService;
import org.csu.gp_store.service.ItemService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class  ItemServiceImpl implements ItemService {

    @Autowired
    private ItemMapper ItemMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private EvaluationService evaluationService;

    @Override
    public List<Item> getItemListByProductID(@NotNull Long productID) {
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("productID",productID);
        return ItemMapper.selectList(queryWrapper);
    }


    @Override
    public int updateItem(Item item) {
        return ItemMapper.updateById(item);
    }
    @Override
    public ItemBO getItemByItemID(@NotNull Long itemID) {
        Item item=ItemMapper.selectById(itemID);
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("productID",item.getProductID());
        Product product=productMapper.selectOne(queryWrapper);
        Company company=companyMapper.selectById(product.getCompanyID());
        List<Evaluation> evaluationList=evaluationService.getEvaluationByProductID(product.getProductID());
        return ItemBO.toItemBO(company,product,item,evaluationList);
    }

    @Override
    public int updateItemStatus(@NotNull Long itemID,int status) {
        UpdateWrapper<Item> updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("itemID",itemID).set("status",status);
        return ItemMapper.update(null,updateWrapper);
    }

    @Override
    public int updateItemPrice(@NotNull Long itemID, BigDecimal price) {
        UpdateWrapper<Item>updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("itemID",itemID).set("price",price);
        return ItemMapper.update(null,updateWrapper);

    }

    @Override
    public int updateItemImage(@NotNull Long itemID,String image) {
        UpdateWrapper<Item>updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("itemID",itemID).set("image",image);
        return ItemMapper.update(null,updateWrapper);

    }

    @Override
    public int deleteItem(Long itemID) {
        return ItemMapper.deleteById(itemID);
    }

    @Override
    public int insertItem(Item item) {
        return ItemMapper.insert(item);
    }
}
